<?php
include_once('../gxpage-functions.php');
include_once('../GXError.php');
include_once('../GXDOM.php');
$GXError = GXError::getInstance();

// load gxdom testing class
$GXDOMTest = new GXDOMTest();
$method = $_GET['m'];
if( !$method ){
	$method = 'GXDOM0';
}

header('content-type: text/xml');
$r = $GXDOMTest->$method();
echo $r->exec->xml();
exit();

class GXDOMTest {

	// source xml document
	public $xmldoc = null;

	public function __construct(){
		$v = array(
			'one'=>array(
				'child'=>'i am a child of one'
				),
			'two'=>'i am the value for two',
			3=>'my node name was numeric',
			'i-am-hyphenated'=>'and my tag name still works',
			' &!@#{}<>'=>'invalid characters cleaned up'
			);
		$obj = new stdClass();
			$obj->one = '1';
			$obj->two = '2';
			$obj->three = '3';
		$data = array(
			$obj,
			$obj,
			$obj
			);
		$data[] = $obj;
		$v[] = $data;
		$this->xmldoc = new GXDOM('root',$v,array('data'=>'GXDOMTest class var','source'=>'constructor'));
		return true;
	}

	// @example 0: show the xml doc created in the constructor of this class
	public function GXDOM0(){
		return $this->xmldoc;
	}

	// @example 1: reading an xml file
	public function GXDOM1(){
		$doc = new GXDOM('../Docs/Data/i18n.xml');
		return $doc;
	}

	// @example 2: loading an xml string
	public function GXDOM2(){
		$doc = new GXDOM('<root source="constructor">this is the root</root>');
		return $doc;
	}

	// @example 3: creating a simple one-node document with attributes
	public function GXDOM3(){
		$doc = new GXDOM('root','i am the text value',array('source'=>'constructor'));
		return $doc;
	}

	// @example 4: creating a one-node document with an xml string for value
	public function GXDOM4(){
		$doc = new GXDOM('root','<xmlstring>this is xml inside the root parsed from a string</xmlstring>',array('source'=>'constructor'));
		return $doc;
	}

	// @example 5: creating a document using a numeric array for value
	public function GXDOM5(){
		$doc = new GXDOM('root',array(1,2,3),array('source'=>'constructor'));
		return $doc;
	}

	// @example 6: creating a document from a PHP object
	public function GXDOM6(){
		$doc = new GXDOM('root',$this,array('source'=>'constructor'));
		return $doc;
	}

	// @example 7: creating a document with a complex recursive array (see constructor)
	public function GXDOM7(){
		return $this->xmldoc;
	}

	// @example 8: loading an array with attributes on the container
	public function GXDOM8(){
		$doc = new GXDOM('root',null,array('source'=>'constructor'));
		$v = array(
			'one'=>array(
				'child'=>'i am a child of one'
				),
			'two'=>'i am the value for two',
			1=>'something',
			2,
			3
			);
		$doc->container();
		//$doc->container( $v, array('parent'=>'root','source'=>'member') );
		return $doc;
	}

	// @example 9: creating a document and adding an array without a containing element
	// can't because xml document must have a root node ... GXDOM creates one automatically
	public function GXDOM9(){
		$doc = new GXDOM();
		$doc->list( array(1,2,3), GXDOM::NO_CONTAINER );
		return $doc;
	}

	// @example 10: getting the results of an xpath
	public function GXDOM10(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '/root' );
		showval($r);
		// run another xpath on the first result of the first xpath ($r2 runs from the first xpath context), and return the nodeName of the first result
		$r2 = $r[0]->exec->xpath( 'one/child', 0 )->nodeName;
		showval($r2);
		// run that same xpath and
		exit();
	}

	// @example 11: adding a child node to the first result of an xpath
	public function GXDOM11(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', 0 );
		$r->baby( 'i am a child of child', array('age'=>'7 months') );
		return $doc;
	}

	// @example 12: replacing the first result of an xpath with a new node
	public function GXDOM12(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', 0 );
		$r->baby( 'i just replaced child and am now a child of one', array('age'=>'7 months'), GXDOM::REPLACE_CALLER );
		return $doc;
	}

	// @example 13: getting the results of an xpath and changing its value using GXDOM
	public function GXDOM13(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', 0 );
		$r->nodeValue = 'my value is now "bang"';
		return $doc;
	}

	// @example 15: getting the results of an xpath and changing its value using DOM
	public function GXDOM14(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', 0, GXDOM::AS_DOM );
		$r->nodeValue = 'my value is now "bang"';
		return $doc;
	}

	// @example 16: getting the results of an xpath and changing its value using SimpleXML
	// BUG WITH SIMPLEXML ... changing the value this way does not affect the document as indicated on www.php.net
	public function GXDOM15(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', GXDOM::AS_SIMPLEXML );
		$r[0]->child = 'my value is now "bang"';
		return $doc;
	}

	// @example 15: add a stylesheet directive
	public function GXDOM16(){
		$doc = $this->xmldoc;
		$doc->exec->styleSheet( 'path/to/stylesheet.xsl' );
		return $doc;
	}

	// @example 16: load a file into a specific spot, replacing an xpath result
	public function GXDOM17(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '//child', 0 );
		$path = realpath( dirname(__FILE__) .'/../Docs/Data/i18n.xml' );
		// path to the file, second param is where to attach it ... it will replace the "to" node
		$r->exec->load($path,GXDOM::REPLACE_CALLER);
		// find the node we're going to replace
		$r = $doc->exec->xpath( '//SiteTable', 0 );
		// create the document which will replace it
		$new = new GXDOM('sub','i am a sub');
		// perform the replacement but without the "new-site-table" container
		$r->{'new-site-table'}($new,GXDOM::NO_CONTAINER,GXDOM::REPLACE_CALLER);
		return $doc;
	}

	// @example 17: get information about a node
	public function GXDOM18(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '/root', 0 );
		showval($r->nodeName .' = ' .$r->nodeValue);
		showval($r->attributes);
		showval($r->attributes['source']);
		exit();
		return $doc;
	}

	// @example 18: generate xml for the whole document
	public function GXDOM19(){
		$doc = $this->xmldoc;
		echo $doc->exec->xml();
		exit();
	}

	// @example 19: generate xml for a particular node
	public function GXDOM20(){
		$doc = $this->xmldoc;
		$r = $doc->exec->xpath( '/root/one', 0 );
		echo $r->exec->xml($r);
		exit();
	}

}

?>